<?php

include_once 'ObraDao.php';
include_once '../commons/Conexion.php';
include_once '../model/Constructora.php';

/**
 * Description of ConstrucDao
 *
 * @author User
 */
class ConstrucDao {

    private $tConstruc = 't001_constructora';

    function __construct() {
        
    }

    /* @var $oConstruc Constructora 
     * @result $oConstruc Constructora
     */

    public function saveConstructora($oConstruc = null) {

        $oConstruc->setId(0);
        try {
            $query = 'insert into %s (co_rif,nb_nombre,tx_direccion,fe_ingreso) values ("%s","%s","%s","%s");';

            $query = sprintf($query, $this->tConstruc, $oConstruc->getRif(), $oConstruc->getNombre(),$oConstruc->getDireccion(), $oConstruc->getFechaIngreso());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);

            $query = 'select l.co_id from ' . $this->tConstruc . ' l where l.nb_nombre = "'
                    . $oConstruc->getNombre() . '" and l.co_rif = "' . $oConstruc->getRif() . '";';

            $result = $conn->query($query);

            while ($fila = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $oConstruc->setId($fila['co_id']);
            }

            $conn->close();
        } catch (Exception $e) {
            
        }

        return $oConstruc;
    }

    /*
     * @var $lId long
     * @result $oConstruc Constructora
     */

    public function getConstrucById($lId = 0) {

        $oConstruc = null;

        try {
            $query = 'select l.* from %s l where l.co_id = %d;';
            $query = sprintf($query, $this->tConstruc, $lId);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            if (isset($result)) {
                $dao = new ObraDao();
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oConstruc = new Constructora();
                    $oConstruc->setId($fila['co_id']);
                    $oConstruc->setRif($fila['co_rif']);
                    $oConstruc->setNombre($fila['nb_nombre']);
                    $oConstruc->setDireccion($fila['tx_direccion']);
                    $oConstruc->setFechaIngreso($fila['fe_ingreso']);
                    $oConstruc->setObras($dao->getObrasByConstruc($fila['co_id']));
                }
            }

            $conn->close();
        } catch (Exception $e) {
            
        }
        return $oConstruc;
    }

    /*
     * @var $sRif string
     * @result $oConstruc Constructora
     */

    public function getConstrucByRIF($sRif = "") {

        $oConstruc = null;

        try {

            $query = 'select l.* from %s l where l.co_rif = %s;';
            $query = sprintf($query, $this->tConstruc, $sRif);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            if (isset($result)) {
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oConstruc = new Constructora();
                    $oConstruc->setId($fila['co_id']);
                    $oConstruc->setRif($fila['co_rif']);
                    $oConstruc->setDireccion($fila['tx_direccion']);
                    $oConstruc->setNombre($fila['nb_nombre']);
                    $oConstruc->setFechaIngreso($fila['fe_ingreso']);
                }
            }

            $conn->close();
        } catch (Exception $e) {
            
        }
        return $oConstruc;
    }

    /*
     * @return $listConstuct List<Constructora>
     */

    public function getConstructoras() {

        $listConstuct = null;

        try {
            $query = 'select l.* from %s l order by l.co_id';
            $query = sprintf($query, $this->tConstruc);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            $listConstuct = array();
            $i = 0;
            while ($fila = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $oConstruc = new Constructora();
                $oConstruc->setId($fila['co_id']);
                $oConstruc->setRif($fila['co_rif']);
                $oConstruc->setDireccion($fila['tx_direccion']);
                $oConstruc->setNombre($fila['nb_nombre']);
                $oConstruc->setFechaIngreso($fila['fe_ingreso']);
                $listConstuct[$i++] = $oConstruc;
            }
            $conn->close();
        } catch (Exception $e) {
            
        }

        return $listConstuct;
    }

    /*
     * @var $oConstruc Constructora
     * @result $oConstruc Constructora
     */

    public function updateConstruc($oConstruc) {

        try {
            $query = 'update %s l set l.co_rif = "%s", l.nb_nombre = "%s", l.tx_direccion = "%s", l.fe_ingreso = "%s"  where  l.co_id = %d;';
            $query = sprintf($query, $this->tConstruc, $oConstruc->getRif(), $oConstruc->getNombre(), $oConstruc->geDireccion(), $oConstruc->getFechaIngreso(), $oConstruc->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);
            $conn->close();
        } catch (Exception $e) {
            $oConstruc = null;
        }

        return $oConstruc;
    }

    /* @var $oConstruc Constructora 
     * @return $result boolean
     */

    public function deleteConstruc($oConstruc) {

        $result = false;
        try {
            $query = 'delete from %s where co_id = %d';
            $query = sprintf($query, $this->tConstruc, $oConstruc->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);
            $conn->close();

            $result = true;
        } catch (Exception $e) {
            
        }

        return $result;
    }

}

?>
